package com.longcai.convert;

import com.alibaba.fastjson.JSON;
import com.longcai.constants.SaoBeiConstants;
import com.longcai.entity.payment.saobei.request.ClyjSaobeiPaymentLogReq;
import com.longcai.entity.payment.saobei.request.ClyjSaobeiPaymentQueryReq;
import com.longcai.util.MD5;
import com.longcai.util.StringUtils;
import com.longcai.vo.payment.saobei.ClyjSaobeiPaymentLogVo;
import com.longcai.vo.payment.saobei.ClyjSaobeiPaymentQuery;
import com.longcai.vo.payment.saobei.ClyjSaobeiPaymentRefund;

/**
 * 扫呗支付 对象转换，参数封装   转json字符串handler
 *
 * @author hyf
 */
public class SaoBeiPaymentHandler {

    /**
     * 2.组装统一下单预支付请求参数
     **/
    public String SaoBeiPaymentLogReq(ClyjSaobeiPaymentLogVo clyjSaobeiPaymentLogVo, String notify_url, String open_id) {
        ClyjSaobeiPaymentLogReq clyjSaobeiPaymentLogReq = SaoBeiPaymentLogconvert(clyjSaobeiPaymentLogVo, open_id);
        return LogToJson(clyjSaobeiPaymentLogReq);
    }

    /**
     * 对象转换，封装请求参数，并转成Json
     **/
    public ClyjSaobeiPaymentLogReq SaoBeiPaymentLogconvert(ClyjSaobeiPaymentLogVo clyjSaobeiPaymentLogVo, String open_id) {
        ClyjSaobeiPaymentLogReq clyjSaobeiPaymentLogReq = new ClyjSaobeiPaymentLogReq();
        clyjSaobeiPaymentLogReq.setPay_type(clyjSaobeiPaymentLogVo.getPayType());
        clyjSaobeiPaymentLogReq.setService_id(clyjSaobeiPaymentLogVo.getServiceId());
        clyjSaobeiPaymentLogReq.setTerminal_trace(clyjSaobeiPaymentLogVo.getTerminalTrace());
        clyjSaobeiPaymentLogReq.setTerminal_time(clyjSaobeiPaymentLogVo.getTerminalTime());
        clyjSaobeiPaymentLogReq.setTotal_fee(clyjSaobeiPaymentLogVo.getTotalFee());
        clyjSaobeiPaymentLogReq.setOpen_id(open_id);
        clyjSaobeiPaymentLogReq.setPay_ver(SaoBeiConstants.PAY_VER);
        clyjSaobeiPaymentLogReq.setMerchant_no(SaoBeiConstants.MERCHANT_NO);
        clyjSaobeiPaymentLogReq.setTerminal_id(SaoBeiConstants.TERMINAL_ID);
        return clyjSaobeiPaymentLogReq;
    }

    /**
     * 对象转json并加签
     **/
    public String LogToJson(ClyjSaobeiPaymentLogReq clyjSaobeiPaymentLogReq) {
        String sig = "pay_ver=" + clyjSaobeiPaymentLogReq.getPay_ver()
                + "&pay_type=" + clyjSaobeiPaymentLogReq.getPay_type()
                + "&service_id=" + clyjSaobeiPaymentLogReq.getService_id()
                + "&merchant_no=" + clyjSaobeiPaymentLogReq.getMerchant_no()
                + "&terminal_id=" + clyjSaobeiPaymentLogReq.getTerminal_id()
                + "&terminal_trace=" + clyjSaobeiPaymentLogReq.getTerminal_trace()
                + "&terminal_time=" + clyjSaobeiPaymentLogReq.getTerminal_time()
                + "&total_fee=" + clyjSaobeiPaymentLogReq.getTotal_fee()
                + "&access_token=" + SaoBeiConstants.TOKEN;
        System.out.println("拼接好的数据：" + sig);
        String sign = MD5.sign(sig, "utf-8");
        clyjSaobeiPaymentLogReq.setKey_sign(sign);
        return JSON.toJSONString(clyjSaobeiPaymentLogReq);
    }


    /**
     * 组装退款申请的参数
     **/
    public String SaoBeiPaymentRefund(ClyjSaobeiPaymentRefund clyjSaobeiPaymentRefund) {
        ClyjSaobeiPaymentLogReq clyjSaobeiPaymentLogReq = SaoBeiPaymentRefundconvert(clyjSaobeiPaymentRefund);
        return RefundToJson(clyjSaobeiPaymentLogReq);
    }

    /**
     * 对象转换，封装请求参数，并转成Json
     **/
    public ClyjSaobeiPaymentLogReq SaoBeiPaymentRefundconvert(ClyjSaobeiPaymentRefund clyjSaobeiPaymentRefund) {
        ClyjSaobeiPaymentLogReq clyjSaobeiPaymentLogReq = new ClyjSaobeiPaymentLogReq();
        clyjSaobeiPaymentLogReq.setPay_type(clyjSaobeiPaymentRefund.getPayType());
        clyjSaobeiPaymentLogReq.setService_id(clyjSaobeiPaymentRefund.getServiceId());
        clyjSaobeiPaymentLogReq.setTerminal_trace(clyjSaobeiPaymentRefund.getTerminalTrace());
        clyjSaobeiPaymentLogReq.setTerminal_time(clyjSaobeiPaymentRefund.getTerminalTime());
        clyjSaobeiPaymentLogReq.setRefund_fee(clyjSaobeiPaymentRefund.getRefund_fee());
        clyjSaobeiPaymentLogReq.setOut_trade_no(clyjSaobeiPaymentRefund.getOut_trade_no());
        clyjSaobeiPaymentLogReq.setPay_ver(SaoBeiConstants.PAY_VER);
        clyjSaobeiPaymentLogReq.setMerchant_no(SaoBeiConstants.MERCHANT_NO);
        clyjSaobeiPaymentLogReq.setTerminal_id(SaoBeiConstants.TERMINAL_ID);
        return clyjSaobeiPaymentLogReq;
    }

    /**
     * 对象转json并加签
     **/
    public String RefundToJson(ClyjSaobeiPaymentLogReq clyjSaobeiPaymentLogReq) {
        String sig = "pay_ver=" + clyjSaobeiPaymentLogReq.getPay_ver()
                + "&pay_type=" + clyjSaobeiPaymentLogReq.getPay_type()
                + "&service_id=" + clyjSaobeiPaymentLogReq.getService_id()
                + "&merchant_no=" + clyjSaobeiPaymentLogReq.getMerchant_no()
                + "&terminal_id=" + clyjSaobeiPaymentLogReq.getTerminal_id()
                + "&terminal_trace=" + clyjSaobeiPaymentLogReq.getTerminal_trace()
                + "&terminal_time=" + clyjSaobeiPaymentLogReq.getTerminal_time()
                + "&refund_fee=" + clyjSaobeiPaymentLogReq.getRefund_fee()
                + "&access_token=" + SaoBeiConstants.TOKEN;
        System.out.println("拼接好的数据：" + sig);
        String sign = MD5.sign(sig, "utf-8");
        clyjSaobeiPaymentLogReq.setKey_sign(sign);
        return JSON.toJSONString(clyjSaobeiPaymentLogReq);
    }


    /**
     * 组装支付查询的参数
     **/
    public String SaoBeiPaymentQuery(ClyjSaobeiPaymentQuery clyjSaobeiPaymentQuery) {
        ClyjSaobeiPaymentQueryReq ClyjSaobeiPaymentQueryReq = SaoBeiPaymentQueryconvert(clyjSaobeiPaymentQuery);
        return QueryToJson(ClyjSaobeiPaymentQueryReq);
    }

    /**
     * 对象转换，封装请求参数，并转成Json
     **/
    public ClyjSaobeiPaymentQueryReq SaoBeiPaymentQueryconvert(ClyjSaobeiPaymentQuery clyjSaobeiPaymentQuery) {
        ClyjSaobeiPaymentQueryReq clyjSaobeiPaymentQueryReq = new ClyjSaobeiPaymentQueryReq();
        clyjSaobeiPaymentQueryReq.setPay_type(clyjSaobeiPaymentQuery.getPayType());
        clyjSaobeiPaymentQueryReq.setService_id(clyjSaobeiPaymentQuery.getPayType());
        clyjSaobeiPaymentQueryReq.setTerminal_time(clyjSaobeiPaymentQuery.getTerminalTime());
        clyjSaobeiPaymentQueryReq.setTerminal_trace(clyjSaobeiPaymentQuery.getTerminalTrace());
        clyjSaobeiPaymentQueryReq.setPay_ver(SaoBeiConstants.PAY_VER);
        clyjSaobeiPaymentQueryReq.setMerchant_no(SaoBeiConstants.MERCHANT_NO);
        clyjSaobeiPaymentQueryReq.setTerminal_id(SaoBeiConstants.TERMINAL_ID);
        /**查询支付结果时参数存在两种情况：1.out_trade_no为空的时候，查询的时候out_trade_no还是需要作为参数(对应的值是"")传进去，
         * 此时pay_trace(对应支付请求中的terminal_trace参数值)和pay_time(对应支付请求中的terminal_time参数值)也要作为请求参数传进去；
         * 2.out_trade_no不为空的时候，直接按照正常操作进行；**/
        if (StringUtils.isEmpty(clyjSaobeiPaymentQuery.getOut_trade_no())) {
            clyjSaobeiPaymentQueryReq.setOut_trade_no("");
            clyjSaobeiPaymentQueryReq.setPay_trace(clyjSaobeiPaymentQuery.getTerminalTrace());
            clyjSaobeiPaymentQueryReq.setPay_time(clyjSaobeiPaymentQuery.getTerminalTime());
        } else {
            clyjSaobeiPaymentQueryReq.setOut_trade_no(clyjSaobeiPaymentQuery.getOut_trade_no());
        }
        return clyjSaobeiPaymentQueryReq;
    }

    /**
     * 对象转json并加签
     **/
    public String QueryToJson(ClyjSaobeiPaymentQueryReq clyjSaobeiPaymentQueryReq) {
        String sig = "pay_ver=" + clyjSaobeiPaymentQueryReq.getPay_ver()
                + "&pay_type=" + clyjSaobeiPaymentQueryReq.getPay_type()
                + "&service_id=" + clyjSaobeiPaymentQueryReq.getService_id()
                + "&merchant_no=" + clyjSaobeiPaymentQueryReq.getMerchant_no()
                + "&terminal_id=" + clyjSaobeiPaymentQueryReq.getTerminal_id()
                + "&terminal_trace=" + clyjSaobeiPaymentQueryReq.getTerminal_trace()
                + "&terminal_time=" + clyjSaobeiPaymentQueryReq.getTerminal_time()
                + "&out_trade_no=" + clyjSaobeiPaymentQueryReq.getOut_trade_no()
                + "&access_token=" + SaoBeiConstants.TOKEN;
        System.out.println("拼接好的数据：" + sig);
        String sign = MD5.sign(sig, "utf-8");
        clyjSaobeiPaymentQueryReq.setKey_sign(sign);
        return JSON.toJSONString(clyjSaobeiPaymentQueryReq);
    }

}